package cn.kzgg.pet.controller;

import cn.kzgg.basic.exception.BusinessException;
import cn.kzgg.basic.util.LoginContext;
import cn.kzgg.org.domain.Employee;
import cn.kzgg.org.service.IEmployeeService;
import cn.kzgg.org.service.IShopService;
import cn.kzgg.pet.dto.ToHandleDto;
import cn.kzgg.pet.service.ISearchMasterMsgService;
import cn.kzgg.pet.domain.SearchMasterMsg;
import cn.kzgg.pet.query.SearchMasterMsgQuery;
import cn.kzgg.basic.util.AjaxResult;
import cn.kzgg.basic.util.PageList;
import cn.kzgg.user.domain.Logininfo;
import cn.kzgg.user.domain.User;
import cn.kzgg.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IUserService userService;

    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.show();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * @param msg
     * @return
     */
    @PostMapping("publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg, HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getCurrentLogininfo(request);
            searchMasterMsgService.publish(msg, logininfo);
            return AjaxResult.show();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.removeById(id);
            return AjaxResult.show();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id") Long id) {
        return searchMasterMsgService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<SearchMasterMsg> list() {

        return searchMasterMsgService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }

    //=================寻主

    /**
     * 待审核
     *
     * @param query
     * @return
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
        query.setState(0);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 待处理
     *
     * @param query
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        //找到处理人
        Logininfo logininfo = LoginContext.getCurrentLogininfo(request);
        Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
        //修改店铺状态占位
        query.setState(1);
        //绑定店铺
        query.setShopId(employee.getShop_id());
        return searchMasterMsgService.queryPage(query);
    }


    /**
     * 已处理
     * @param query
     * @return
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query) {
        //修改店铺状态占位
        query.setState(2);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 用户寻主列表-个人中心-我的消息
     * @param query
     * @return
     */
    @PostMapping("/mine")
    public PageList<SearchMasterMsg> mine(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        //获取当前登陆人
        Logininfo logininfo = LoginContext.getCurrentLogininfo(request);
        User user = userService.loadByLogininfoId(logininfo.getId());
        query.setUserId(user.getId());
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 寻主池
     * @param query
     * @return
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query) {
        query.setState(1);//审核通过但是没有找到对应的shop
        return searchMasterMsgService.queryPage(query);
    }


    /**
     * 接单
     * @param dto
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody ToHandleDto dto) {
        try {
            searchMasterMsgService.accept(dto);
            return AjaxResult.show();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 拒收
     * @param id
     * @return
     */
    @DeleteMapping(value = "/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.show();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

}
